﻿var virualpath = "../../"
$(function () {
    layui.use(['layer', 'form'], function () {
        var layer = layui.layer
            , form = layui.form;
        form.render();
        //当前站点虚拟路径
       
        console.log(1)
        $.ajax({
            url: virualpath + '/Login/GetUserLoginNum',
            type: 'get',
            dataType: 'json',
            async: true,
            contentType: 'application/json',
            success: function (data) {
                var result = data;
                if (result != undefined) {
                    $("#SignInNum").text("已有" + (1500+Number(Object.keys(data).length)) + "人次访问")
                }
            }
        })

        $("#Login_Btn").click(function () {
            var field = {
                AdminName: $("#username").val(),
                Password: $("#password").val()
            }

            if (field.AdminName=='') {
                layer.msg("账号不能为空")
                return;
            }

            if (field.Password =='') {
                layer.msg("密码不能为空")
                return;
            }

            $.ajax({
                url: virualpath+ '/Login/SignIn',
                type: 'post',
                dataType: 'json',
                data: JSON.stringify(field),
                contentType: 'application/json',
                async: true,
                success: function (data) {
                    if (data.RspCode == 1) {
                        var index = layer.msg('登录中，请稍后', { icon: 16, time: false, shade: 0.8 });
                        setTimeout(function () {
                            form.render();
                            GetLoginSignIn();
                            layer.close(index)
                            layer.msg(data.RspMsg)
                            //var name = JSON.parse(res).res_data[0].LinkName;
                            //window.sessionStorage.setItem("Name", name)
                            window.location.href = "index.html";
                        }, 500)
                    }
                    else {
                        var index = layer.msg('登录中，请稍后', { icon: 16, time: false, shade: 0.8 });
                        setTimeout(function () {
                            form.render();
                            layer.close(index)
                            alert(data.RspMsg)
                            window.location.reload()
                        }, 500)
                    }
                  

                }
            })

        })
    });
})

function GetLoginSignIn() {
    $.ajax({
        url: virualpath + '/Session/GetSessionAsync',
        type: 'get',
        async: true,
        contentType: 'application/json',
        success: function (data) {
            var res = JSON.parse(data);
            if (res != undefined) {
                InsertLoginSignIn(res.Id)
            }
        }
    })
}

function InsertLoginSignIn(id) {
    var obj = {}
    obj.Id = id;
    $.ajax({
        url: virualpath + '/Login/InsertSignInInfo',
        type: 'post',
        data: JSON.stringify(obj),
        async: true,
        contentType: 'application/json',
        success: function (d) {
            
        }
    })
}